package com.wdl.modules.myhouse.controller;

import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dto.CommentsDTO;
import com.wdl.modules.myhouse.entity.CommentsEntity;
import com.wdl.modules.myhouse.service.CommentsService;
import com.wdl.modules.myhouse.vo.CommentsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 评论表
 *
 * @author lucain sunlightcs@gmail.com
 * @since 1.0.0 2022-10-06
 */
@RestController
@RequestMapping("/comments")
@Api(tags = "评论表")
@Slf4j
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @ApiOperation(value = "保存文件信息")
    @PostMapping("/save")
    private Result<Void> save(@RequestBody CommentsDTO comment) {
        log.info("保存评论信息", comment.toString());
        CommentsEntity entity = new CommentsEntity();
        BeanUtils.copyProperties(comment, entity);
        commentsService.save(entity);
        return new Result<Void>().ok();
    }

    @ApiOperation(value = "保存文件信息")
    @GetMapping("/file/{id}")
    private Result<List<CommentsVo>> getCommentsByFileId(@PathVariable("id") Long fileId) {
        log.info("通过文件id获取相关评论信息", fileId);
        return new Result<List<CommentsVo>>().setData(commentsService.getComments(fileId));
    }


    @ApiOperation(value = "获取最早的几条评论")
    @GetMapping("/latest/{num}")
    private Result<List<CommentsVo>> getLastestComment(@PathVariable("num") Integer num) {
        return new Result<List<CommentsVo>>().ok().setData(commentsService.getLastestComment(num));
    }


}